/*================================================================
*   File Name：device_hash.c
*   Mail: a1277992967@163.com 
*   blog: http://www.ycst.club/suggest/ 
*   Author：ycst
*   Created Time：2022年08月16日 星期二 11时27分06秒
*   Describe：
*
================================================================*/

#include "../include/device_hash.h"

Hash *CreateHash(){
	Hash *pHash = (Hash *)malloc(sizeof(Hash));
	if(NULL == pHash)	return NULL;
	memset(pHash,0,sizeof(Hash));
	return pHash;
}
void DestroyHash(Hash *pHash){
	if(NULL == pHash)	return;
	int i = 0;
	for(i = 0;i < HASH_SIZE ; i++){
		if(NULL == pHash->table[i])	continue;
		Node *pTmp = NULL;
		Node *pDel = pHash->table[i];
		while(NULL != pDel){
			pTmp = pDel->pNext;
			free(pDel);
			pDel = pTmp;
		}
	}	
	free(pHash);
	pHash = NULL;
}
int InsertItem(Hash *pHash, data_t stInfo){
	if(NULL == pHash || 0 == strlen(stInfo.strDeviceID))	return -1;
	Node *pNode = (Node *)malloc(sizeof(Node));
	if(NULL == pNode)	return -1;	
	memset(pNode,0,sizeof(Node));
	pNode->data = stInfo;
	
	int index = atoi(stInfo.strDeviceID) % 100;
	
	pNode->pNext = pHash->table[index];
	pHash->table[index] = pNode;

	return 0;
}
int DeleteItem(Hash *pHash, data_t tData){
	if(NULL == pHash || 0 == strlen(tData.strDeviceID))	return -1;
	int index = atoi(tData.strDeviceID) % 100;
	Node *pDel = pHash->table[index];
	if(NULL == pDel)	return -1;
	if(strcmp(pDel->data.strDeviceID,tData.strDeviceID) == 0){
		pHash->table[index] = pDel->pNext;
		free(pDel);
		pDel = NULL;
		return 1;
	}
	else{
		Node *pTmp = pHash->table[index];
		while(NULL != pTmp->pNext){
			pDel = pTmp->pNext;
			if(strcmp(pDel->data.strDeviceID,tData.strDeviceID) == 0){
				pTmp->pNext = pDel->pNext;
				free(pDel);
				pDel = NULL;
				return 2; 
			}
			pTmp = pDel;
		}
	}
	
	return 0;
}
int UpdateItem(Hash *pHash, data_t tData){
	if(NULL == pHash)	return SYS_ERROR;
	int i = 0;
	Node *pTmp = NULL;
	for(;i < HASH_SIZE;i++){
		pTmp = pHash->table[i];
		if(NULL == pTmp)	continue;
		while(NULL != pTmp)
		{
			if(strcmp(pTmp->data.strDeviceID, tData.strDeviceID) == 0){
				if(pTmp->data.iFd == tData.iFd){
					//printf("difftime = %ld\r\n", );
					if(time(NULL) - pTmp->data.lTime > 60 * 10){
						return SYS_ERROR;
					}
					else{
						pTmp->data.lTime = time(NULL);
						return SYS_OK;
					}
				}
				return SYS_DEVICE_REINSERT;
			}
			pTmp = pTmp->pNext;
		}
	}
}
int FindDevice(Hash *pHash, data_t tData, int *pOldFd){
	if(pHash == NULL)	return SYS_ERROR;
	int i = 0;
	Node *pTmp = NULL;
	for(;i < HASH_SIZE;i++){
		pTmp = pHash->table[i];
		if(NULL == pTmp)	continue;
		while(NULL != pTmp)
		{
			if(strcmp(pTmp->data.strDeviceID, tData.strDeviceID) == 0){
				if(pTmp->data.iFd == tData.iFd){
					//pTmp->data.lTime = time(NULL);
					return SYS_OK;
				}
				*pOldFd = pTmp->data.iFd;
				return SYS_DEVICE_REINSERT;
			}
			pTmp = pTmp->pNext;
		}
	}
}
void ShowHash(Hash *pHash){
	if(NULL == pHash)	return;
	int i = 0;
	Node *pTmp = NULL;
	for(;i < HASH_SIZE;i++){
		pTmp = pHash->table[i];
		if(NULL == pTmp)	continue;
		while(NULL != pTmp)
		{
			printf("iFd = %d ID = %s Time = %ld\n", pTmp->data.iFd, pTmp->data.strDeviceID, pTmp->data.lTime);
			pTmp = pTmp->pNext;
		}
	}
}
int DeleteTimeOutDevFromHash(Hash *pHash, fd_set *pSet, int iMax){
	if(pHash == NULL || pSet == NULL)	return -1;
}
int FindFdFromHash(Hash *pHash, char *dev_id){
	if(NULL == pHash)	return -1;
	int i = 0;
	Node *pTmp = NULL;
	for(;i < HASH_SIZE;i++){
		pTmp = pHash->table[i];
		if(NULL == pTmp)	continue;
		while(NULL != pTmp)
		{
			if(strcmp(dev_id, pTmp->data.strDeviceID) == 0){
				return pTmp->data.iFd;
			}
			pTmp = pTmp->pNext;
		}
	}
}
















